package com.example.demo.config;

import com.example.demo.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ResponseBody
@ControllerAdvice
@Slf4j
public class ExceptionAdvice {
    @ExceptionHandler
    public Result handlerException(Exception e) {
        log.error("发生异常,e:", e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler
    public Result handlerException(NullPointerException e) {
        log.error("发生异常,e:", e);
        return Result.fail("发生空指针异常: " + e.getMessage());
    }

    @ExceptionHandler
    public Result handlerException(ArithmeticException e) {
        log.error("发生异常,e:", e);
        return Result.fail("发生算数异常: " + e.getMessage());
    }
}


//@ResponseBody
//@ControllerAdvice
//@Slf4j
//public class ExceptionAdvice {
//    @ExceptionHandler
//    public Result handlerException(Exception e) {
//        log.error("发生异常,e:", e);
//        return Result.fail("内部错误");
//    }
//
//    @ExceptionHandler
//    public Result handlerException(NullPointerException e) {
//        log.error("发生异常,e:", e);
//        return Result.fail("发生空指针异常");
//    }
//
//    @ExceptionHandler
//    public Result handlerException(ArithmeticException e) {
//        log.error("发生异常,e:", e);
//        return Result.fail("发生算数异常");
//    }
//}
